home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gnu / adainc / s-wchwts.adb < prev    next >
Text File  |  1996-01-30  |  4KB  |  104 lines

  1. ------------------------------------------------------------------------------
  2. --                                                                          --
  3. --                         GNAT RUNTIME COMPONENTS                          --
  4. --                                                                          --
  5. --                       S Y S T E M . W C H _ W T S                        --
  6. --                                                                          --
  7. --                                 B o d y                                  --
  8. --                                                                          --
  9. --                            $Revision: 1.5 $                              --
  10. --                                                                          --
  11. --           Copyright (c) 1992,1993,1994 NYU, All Rights Reserved          --
  12. --                                                                          --
  13. -- The GNAT library is free software; you can redistribute it and/or modify --
  14. -- it under terms of the GNU Library General Public License as published by --
  15. -- the Free Software  Foundation; either version 2, or (at your option) any --
  16. -- later version.  The GNAT library is distributed in the hope that it will --
  17. -- be useful, but WITHOUT ANY WARRANTY;  without even  the implied warranty --
  18. -- of MERCHANTABILITY  or  FITNESS FOR  A PARTICULAR PURPOSE.  See the  GNU --
  19. -- Library  General  Public  License for  more  details.  You  should  have --
  20. -- received  a copy of the GNU  Library  General Public License  along with --
  21. -- the GNAT library;  see the file  COPYING.LIB.  If not, write to the Free --
  22. -- Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.        --
  23. --                                                                          --
  24. ------------------------------------------------------------------------------
  25.  
  26. with System.WCh_Con; use System.WCh_Con;
  27. with System.WCh_JIS; use System.WCh_JIS;
  28.  
  29. package body System.WCh_WtS is
  30.  
  31.    ---------------------------
  32.    -- Wide_String_To_String --
  33.    ---------------------------
  34.  
  35.    function Wide_String_To_String
  36.      (S    : Wide_String;
  37.       EM   : WC_Encoding_Method)
  38.       return String
  39.    is
  40.       R  : String (1 .. 5 * S'Length); -- worst case length!
  41.       RP : Natural;
  42.       B1 : Natural;
  43.       B2 : Natural;
  44.       C1 : Character;
  45.       C2 : Character;
  46.  
  47.    begin
  48.       RP := 0;
  49.  
  50.       for SP in S'Range loop
  51.          declare
  52.             C   : constant Wide_Character := S (SP);
  53.             CV  : constant Natural        := Wide_Character'Pos (C);
  54.             Hex : constant array (0 .. 15) of Character := "0123456789ABCDEF";
  55.  
  56.          begin
  57.             if CV <= 127
  58.               or else (CV <= 255 and then EM = WCEM_Hex) then
  59.                RP := RP + 1;
  60.                R (RP) := Character'Val (CV);
  61.  
  62.             else
  63.                B1 := CV / 256;
  64.                B2 := CV mod 256;
  65.  
  66.                --  Hex ESC sequence encoding
  67.  
  68.                if EM = WCEM_Hex then
  69.                   R (RP + 1) := Ascii.ESC;
  70.                   R (RP + 2) := Hex (B1 / 16);
  71.                   R (RP + 3) := Hex (B1 rem 16);
  72.                   R (RP + 4) := Hex (B2 / 16);
  73.                   R (RP + 5) := Hex (B2 rem 16);
  74.                   RP := RP + 5;
  75.  
  76.                --  Upper bit shift (internal code = external code)
  77.  
  78.                elsif EM = WCEM_Upper then
  79.                   C1 := Character'Val (B1);
  80.                   C2 := Character'Val (B2);
  81.  
  82.                --  Upper bit shift (EUC)
  83.  
  84.                elsif EM = WCEM_EUC then
  85.                   JIS_To_EUC (C, C1, C2);
  86.  
  87.                --  Upper bit shift (Shift-JIS)
  88.  
  89.                else -- EM = WCEM_Shift_JIS
  90.                   JIS_To_Shift_JIS (C, C1, C2);
  91.                end if;
  92.  
  93.                R (RP + 1) := C1;
  94.                R (RP + 2) := C2;
  95.                RP := RP + 2;
  96.             end if;
  97.          end;
  98.       end loop;
  99.  
  100.       return R (1 .. RP);
  101.    end Wide_String_To_String;
  102.  
  103. end System.WCh_WtS;
  104.